﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nim.IO;
using Nim.AI;
using System.Threading;

namespace Nim.Game
{
    class NimGame
    {
        private bool gameOver, player;
        private string playerName;

        private NimBoard board;
        private NimAI hal;

        private int mode;

        public NimGame()
        {
            gameOver = false;
            player = false;
            board = new NimBoard();
            hal = new NimAI();
        }

        //Initializes the Board and AI
        public void Initialize()
        {
            board.Initialize();
            hal.Initialize();
        }

        //Starts the game
        public void StartGame(int mode)
        {
            this.mode = mode;
            while (!gameOver)
            {
                SwitchPlayer();
                if (mode == 1 || mode == 2) TakeTurn();
                if (mode == 3)
                {
                    AITurn();
                }

                if (!board.IsBoardEmpty())
                {
                    if (mode == 2 || mode == 3)
                    {
                        SwitchPlayer();
                        AITurn();
                    }
                }

                if (board.IsBoardEmpty()) GameOver();
            }
        }

        //Switches the player variable and playerName variable
        private void SwitchPlayer()
        {
            player = !player;

            if (player) playerName = "Player1";
            else playerName = "Player2";
        }

        //A turn for a person
        private void TakeTurn()
        {
            bool canTake = false;
            Console.Clear();
            ShowBoard();

            string state = board.GetBoardState();

            do
            {
                int row = KeyBoard.GetInt("What row would you like to take from? 1-3");
                int toTake = KeyBoard.GetInt("How many would you like to take from row " + row + "?");

                canTake = board.TakeFrom(row - 1, toTake);

                if (!canTake) Console.WriteLine("You can't take {0} sticks from row {1} try again...", toTake, row);
                else hal.WatchMove(state, row, toTake, player);

            } while (!canTake);

        }

        //A turn for the AI
        private void AITurn()
        {
            bool canTake = false;
            string state = board.GetBoardState();
            int row = 0;
            int toTake = 0;

            Console.Clear();
            ShowBoard();

            do
            {
                NimMove move = hal.MakeMove(state);
                row = move.Row;
                toTake = move.Number;

                canTake = board.TakeFrom(row - 1, toTake);

                if (canTake) hal.WatchMove(state, row, toTake, player);
            } while (!canTake);

            Console.WriteLine("AI Move: {0},{1}", row, toTake);
            Thread.Sleep(1000);
        }

        //Shows the board
        private void ShowBoard()
        {
            Console.WriteLine("It's {0}'s turn.", playerName);
            Console.WriteLine("Here's the board: ");
            board.DrawBoard();
        }

        //Stops the game loop
        //Shows who won the game
        private void GameOver()
        {
            gameOver = true;
            SwitchPlayer();
            Console.WriteLine("{0} WINS!",playerName);
            hal.EvaluateGame(player);
        }
    }
}
